package top.leinx.fxnotes.control;

/**
 * @Author : xiaolei
 * @DateTime: 2021/3/28 1:32 下午
 */

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.time.LocalDate;

public class ComBoxExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        // String 类型的组合框
        ComboBox<String> comboBoxNames = new ComboBox<>();        // 创建空的字符串组合框
        comboBoxNames.getItems().addAll("张三", "李四", "王五");  // 添加3个备选项

        CheckBox checkBoxEditable = new CheckBox("可编辑");       // 创建一个复选框表示 comboBoxNames 是否可编辑
        // comboBoxNames 的 editable 属性与 checkBoxEditable 的 selected 属性绑定
        comboBoxNames.editableProperty().bind(checkBoxEditable.selectedProperty());

        Button buttonNames = new Button("输出选中姓名");
        buttonNames.setOnAction(event -> {
            System.out.println("选中的姓名是: " + comboBoxNames.getValue());
        });

        ToolBar toolBarNames = new ToolBar(comboBoxNames, checkBoxEditable, buttonNames);

        // Student 类型的组合框
        ComboBox<Student> comboBoxStudents = new ComboBox<>();
        comboBoxStudents.setPromptText("选择一个学生");
        comboBoxStudents.getItems().addAll(
                new Student("1", "张三", "计算机"),
                new Student("2", "李四", "软件工程"),
                new Student("3", "王五", "计算机"));
        comboBoxStudents.setOnAction(event -> {
            Student selected = comboBoxStudents.getValue();
            if (selected != null) {
                System.out.println("选中的学生：" + selected.toString() + " , " + selected.getMajor());
            }
        });

        TextField textFieldStudentName = new TextField();
        textFieldStudentName.setPromptText("输入需要设置的学生姓名并回车");
        textFieldStudentName.setOnAction(event -> {
            String name = textFieldStudentName.getText();
            for (Student student : comboBoxStudents.getItems()) {
                if (student.getName().equals(name)) {
                    comboBoxStudents.setValue(student);
                    return;
                }
            }
            comboBoxStudents.setValue(null);
        });

        ToolBar toolBarStudent = new ToolBar(comboBoxStudents, textFieldStudentName);

        // DatePicker 类使用
        DatePicker datePicker = new DatePicker();              // 创建默认日期选择器
        datePicker.setValue(LocalDate.now());                  // 设置当前选中日期为当前日期
        // DatePicker datePicker = new DatePicker(LocalDate.now()); // 创建以当前日期为 value 的日期选择器
        datePicker.setOnAction(event -> {
            System.out.println("选中日期：" + datePicker.getValue());
        });
        Label labelSelectDate = new Label();
        labelSelectDate.textProperty().bind(datePicker.valueProperty().asString()); // 绑定，显示选中的日期
        ToolBar toolBarDatePicker = new ToolBar(datePicker, labelSelectDate);

        // ColorPicker 类使用
        ColorPicker colorPicker = new ColorPicker();
        colorPicker.setOnAction(event -> {
            System.out.println("选中颜色" + colorPicker.getValue());
        });
        ToolBar toolBarColorPicker = new ToolBar(colorPicker);

        VBox vBox = new VBox(toolBarNames, toolBarStudent, toolBarDatePicker, toolBarColorPicker);

        primaryStage.setTitle("组合框系列示例");
        primaryStage.setScene(new Scene(vBox, 400, 200));
        primaryStage.show();
    }
}
